import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { Component,Injector } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular';

/**
 * Generated class for the PayCashDepositPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-pay-cash-deposit',
  templateUrl: 'pay-cash-deposit.html',
})
export class PayCashDepositPage {

  /*用来保存当前优惠券是否有效
    true 表示有效
    false 表示失效
  */
  private isValid:boolean;

  /*优惠券的有效时间*/
  private validTime:Number;
  private userinfo:any;
  /*保存当前用户是否有 这个优惠券*/
  private isHasyouhuijuan:boolean;

  param={
    shop_id:1,
    pay_type:1
  };

  /*使用代金劵支付的必须的参数*/
  payShopBondParam={
    user_id:null,
    order_sn:null,
    use_code:null,
  };


  get alertCtrl():AlertController {return this.injector.get(AlertController);};

  constructor(public navCtrl: NavController,
    public injector:Injector,
    public userService:UserService,
    
    public stc: StorageService,
    public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PayCashDepositPage');
    this.validTime = new Date('2019-2-1').getTime();

    console.log(this.validTime);
    
    /*表示当前的时间*/
    let nowTime = new Date().getTime();

    if(this.validTime>nowTime) this.isValid = true;
    else this.isValid = false;

    console.log(this.navParams.data.shop_id);
    this.userinfo = this.stc.read('userinfo');
    this.param.shop_id = this.userinfo.shop_id;
    
    console.log(this.param);

    /*获取订单号*/
    this.orderSn(); 
    console.log(this.payShopBondParam);

  }
  async orderSn(){
    let res = await this.userService.generateBondOrderAPI(this.param);
    console.log(res);
    if(res.code == 0 || res.code== 1 ){
      console.log("生成了订单");
      if(res.data.coupon==0){
        console.log("没有代金劵");
        this.isHasyouhuijuan = false;
      }else{
        console.log("生成的代金劵");
        this.isHasyouhuijuan = true;
        this.payShopBondParam.use_code = res.data.coupon.use_code;
        this.payShopBondParam.order_sn = res.data.order_sn;  
        this.payShopBondParam.user_id = this.userinfo.user_id;
      }
    }else{
      console.log("网络错误");
      this.showAlert(res.msg);
    }
  }

  /*使用优惠券付款*/
  async useyouhuijuan(){
    this.showConfirm(' ','使用优惠劵付款',this.gotoPlay);
  }


  async gotoPlay(){
    console.log("点击了确定用优惠券付款");
    let res = await this.userService.payShopBondAPI(this.payShopBondParam);
    if(res.code == 1){
      this.showAlert(res.msg);
      this.isValid = false;
    }else{
      this.showAlert(res.msg);
    }
  }


  /*显示提示的功能*/
  showAlert(msg, title = '提示', callFunc = null) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: msg,
      buttons: [
        {
          text: "确定",
          handler: data => {
            if (callFunc) {
              callFunc.apply(data);
            }
          }
        }]
    });
    alert.present();
  }

  /*显示一个有取消 按钮的提示*/
  showConfirm(msg, title = "提示", callFunc: any = null) {
    let confirm = this.alertCtrl.create({
      title: title,
      message: msg,
      buttons: [

        {
          text: '取消',
          handler: () => {
            console.log('Agree clicked');
          }
        },
        {
          text: '确定',
          handler: data => {
            this.gotoPlay();
          }
        },
      ]
    });
    confirm.present();
  }


}
